﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using UnityEngine;
using UnitySheet;

public class SheetContent_XML : MonoBehaviour {
    public static void SheetContent2XML(Sheet sheet,XmlDocument xmlDoc, XmlElement mainElement)
    {
        List<ContentCell> cells = new List<ContentCell>();
        foreach (var item in sheet.cells.Values)
        {
            cells.Add((ContentCell)item);
        }
        
        XmlElement sheetContents = xmlDoc.CreateElement("sheetContents");
        foreach (var item in cells)
        {
            XmlElement content = xmlDoc.CreateElement("content");
            XmlElement cType = xmlDoc.CreateElement("contentType");
            XmlElement cellIndex = xmlDoc.CreateElement("cellIndex");
            cellIndex.InnerText = item.anchorPoints[0].x + ":" + item.anchorPoints[0].y;
            bool isNull = item.cellContent == null;

            cType.InnerText = isNull ? ((int)CellContent.CellContentType.N).ToString() : ((int)item.cellContent.contentType).ToString();
            XmlElement description = xmlDoc.CreateElement("description");
            description.InnerText = !isNull && item.cellContent.contentType == CellContent.CellContentType.Text ? item.cellContent.description : string.Empty;
            XmlElement optionIndex = xmlDoc.CreateElement("optionIndex");
            StringBuilder sb = new StringBuilder();
            if (!isNull && item.cellContent.contentType == CellContent.CellContentType.DropDown)
            {
                foreach (var v in ((OptionCellContent)item.cellContent).Values)
                {
                    sb.Append(v.myOption.number + ":" + v.number+";");
                }
            }
            else
            {
                sb.Append(string.Empty);
            }
            optionIndex.InnerText = sb.ToString();

            content.AppendChild(cellIndex);
            content.AppendChild(cType);
            content.AppendChild(description);
            content.AppendChild(optionIndex);
            sheetContents.AppendChild(content);
        }
        mainElement.AppendChild(sheetContents);
    }
    public static void XML2SheetContents(string xmlPath,Sheet sheet,string mainNodeName)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlPath);
        XmlNode sheetContents = xmlDoc.SelectSingleNode(mainNodeName).SelectSingleNode("sheetContents");
        XmlNodeList contents = sheetContents.SelectNodes("content");
        foreach (XmlElement item in contents)
        {
            CellContent.CellContentType ct = (CellContent.CellContentType)int.Parse(item.GetElementsByTagName("contentType")[0].InnerText);
            CellContent cellContent;
            int x, y;
            string[] cellIndex = item.GetElementsByTagName("cellIndex")[0].InnerText.Split(':');
            x = int.Parse(cellIndex[0]);
            y = int.Parse(cellIndex[1]);
            switch (ct)
            {
                case CellContent.CellContentType.Text:
                    cellContent = new TextCellContent();
                    cellContent.description = item.GetElementsByTagName("description")[0].InnerText;
                    break;
                case CellContent.CellContentType.DropDown:
                    cellContent = new OptionCellContent();
                    
                    string[] s = item.GetElementsByTagName("optionIndex")[0].InnerText.Split(new string[] { ";" }, System.StringSplitOptions.RemoveEmptyEntries);
                    foreach (var indexPair in s)
                    {
                        string[] index = indexPair.Split(':');
                        ((OptionCellContent)cellContent).SetOption((OptionContent_String)((OptionContent_Option)SheetSystemManager.sheetSystemManager.optionsManager.optionGroupList[int.Parse(index[0])]).content[int.Parse(index[1])], true);
                    }
                    break;
                default:
                    cellContent = null;
                    break;
            }
            ((ContentCell)sheet.cells[sheet.anchors[x][y]]).SetContent(cellContent);
        }
    }
}
